package com.sms.platform.mq;

import com.sms.platform.common.constants.RabbitMqConsants;
import com.sms.platform.common.model.Standard_Report;
import com.sms.platform.service.QueueService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.CustomizableThreadFactory;
import org.springframework.stereotype.Component;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 使用多线程推送短信状态
 *
 * @author xjh
 * @date 2020-08-14
 */
@Component
@Slf4j
public class SmsReportReceiver {

    @Autowired
    QueueService queueService;

    @RabbitListener(queues = RabbitMqConsants.TOPIC_PUSH_SMS_REPORT, autoStartup = "true")
    public void receiveStandardReport(Standard_Report report){

        new ThreadFactory() {
            @Override
            public Thread newThread(Runnable r) {
                return null;
            }
        };
        ThreadFactory topicSmsReportThreadFactory = new CustomizableThreadFactory("topicSmsReportThread-pool-");

        ThreadPoolExecutor threadPool = new ThreadPoolExecutor(5, 10, 3, TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(20), topicSmsReportThreadFactory);

        log.error("StandardReport：{}",report);
        Runnable runnable = () -> queueService.pushStatusReport(report);
        threadPool.execute(runnable);

    }
}